package com.yueya.basic.token.service;

import com.alicloud.openservices.tablestore.model.Column;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.DeleteRowRequest;
import com.alicloud.openservices.tablestore.model.DeleteRowResponse;
import com.alicloud.openservices.tablestore.model.GetRowRequest;
import com.alicloud.openservices.tablestore.model.GetRowResponse;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.PutRowRequest;
import com.alicloud.openservices.tablestore.model.Row;
import com.alicloud.openservices.tablestore.model.RowDeleteChange;
import com.alicloud.openservices.tablestore.model.RowPutChange;
import com.alicloud.openservices.tablestore.model.RowUpdateChange;
import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;
import com.alicloud.openservices.tablestore.model.UpdateRowRequest;
import com.yueya.common.util.UUIDGenerator;
import com.yueya.config.GlobalParam;
import com.yueya.config.TableKit;

/**
 * 
 * 
 * @author yangfeng
 * 
 */
public class TokenService {

	public String add() {
		String token = UUIDGenerator.getUUID();
		// aliyun table start

		// 构造主键
		PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
		primaryKeyBuilder.addPrimaryKeyColumn(TableKit.COLUMN_PK, PrimaryKeyValue.fromString(token));
		PrimaryKey primaryKey = primaryKeyBuilder.build();

		// add
		long now = System.currentTimeMillis();
		
		RowPutChange rowPutChange = new RowPutChange(TableKit.TOKEN, primaryKey);
		rowPutChange.addColumn(
				new Column(TableKit.COLUMN_EXPIRES, ColumnValue.fromLong(now + GlobalParam.getTokenExpire())));
		TableKit.getClient().putRow(new PutRowRequest(rowPutChange));

		// aliyun table stop

		return token;
	}

	public void update(String token) {

		// aliyun table start

		// 构造主键
		PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
		primaryKeyBuilder.addPrimaryKeyColumn(TableKit.COLUMN_PK, PrimaryKeyValue.fromString(token));
		PrimaryKey primaryKey = primaryKeyBuilder.build();

		// update
		long now = System.currentTimeMillis();
		RowUpdateChange rowUpdateChange = new RowUpdateChange(TableKit.TOKEN, primaryKey);
		rowUpdateChange.put(TableKit.COLUMN_EXPIRES, ColumnValue.fromLong(now + GlobalParam.getTokenExpire()));
		TableKit.getClient().updateRow(new UpdateRowRequest(rowUpdateChange));
		
		// aliyun table stop
	}

	public boolean delete(String token) {
		boolean bl = false;

		// 构造主键
		PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
		primaryKeyBuilder.addPrimaryKeyColumn(TableKit.COLUMN_PK, PrimaryKeyValue.fromString(token));
		PrimaryKey primaryKey = primaryKeyBuilder.build();

		DeleteRowRequest request = new DeleteRowRequest();
		RowDeleteChange rowChange = new RowDeleteChange(TableKit.TOKEN, primaryKey);
		request.setRowChange(rowChange);

		DeleteRowResponse result = TableKit.getClient().deleteRow(request);

		int consumedWriteCU = result.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit();

		System.out.println("成功删除数据, 消耗的写CapacityUnit为：" + consumedWriteCU);
		return bl;
	}

	/**
	 * 校验TOKEN有效性
	 * 
	 * @param token
	 * @return
	 */
	public boolean checkToken(String token) {
		boolean bl = false;
		// 构造主键
		PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
		primaryKeyBuilder.addPrimaryKeyColumn(TableKit.COLUMN_PK, PrimaryKeyValue.fromString(token));
		PrimaryKey primaryKey = primaryKeyBuilder.build();

		// 读一行
		SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TableKit.TOKEN, primaryKey);
		// 设置读取最新版本
		criteria.setMaxVersions(1);
		GetRowResponse getRowResponse = TableKit.getClient().getRow(new GetRowRequest(criteria));
		Row row = getRowResponse.getRow();

		if (row != null) {

			Column columnExpires = row.getLatestColumn(TableKit.COLUMN_EXPIRES);
			ColumnValue cv = columnExpires.getValue();
			long expires = cv.asLong();
			long now = System.currentTimeMillis();
			if (expires >= now) {
				bl = true;
			}
		}
		return bl;
	}

	public static void main(String args[]) {
		System.out.println(System.currentTimeMillis());
	}
	
}
